package com.netease.xone.itemview;

import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.widget.richtext.DiscussTextView;
import com.netease.xone.widget.richtext.IRichTextClickListener;


public class RenderInfoDiscuss implements IRenderInfoBase, OnClickListener, IRichTextClickListener{
    private static final String TAG = RenderInfoDiscuss.class.getSimpleName();
    
    private boolean bForward = false;
    private int ID = 0x8888;
    private int mPadding;
    private View mRoot;
    private DiscussTextView mContent;
    private LinearLayout mAudio;
    private LinearLayout mImage;

    public RenderInfoDiscuss(View v) {
        mRoot = v;
        
        mContent = (DiscussTextView)v.findViewById(R.id.discuss_content);
        mAudio = (LinearLayout)v.findViewById(R.id.discuss_audio);
        mImage = (LinearLayout)v.findViewById(R.id.discuss_image);
        
        mContent.setRichTextClickListener(this);
        mPadding = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_10);
    }

    public void setForward(boolean flag) {
        bForward = flag;
    }
    
    private void setContent(String value) {
        if (TextUtils.isEmpty(value)) {
            mContent.setVisibility(View.GONE);
        } else {
            mContent.setVisibility(View.VISIBLE);
            if (bForward) {
                mContent.setEnable(IRichTextClickListener.ENABLE_RICHTEXT_NAME);
            }
            mContent.setRichText(value);
        }
    }
    
    protected IRenderInfoBase getAudio() {
        IRenderInfoBase iRenderInfoBase = null;
        
        if (null != mAudio.getTag() && mAudio.getTag() instanceof RenderInfoAudio) {
            iRenderInfoBase = (IRenderInfoBase)mAudio.getTag();
        } else {
            iRenderInfoBase = new RenderInfoAudio(mAudio);
            mAudio.setTag(iRenderInfoBase);
        }
        
        return iRenderInfoBase;
    }
    
    protected IRenderInfoBase getImage() {
        IRenderInfoBase iRenderInfoBase = null;
        
        if (null != mImage.getTag() && mImage.getTag() instanceof RenderInfoImageItem) {
            iRenderInfoBase = (IRenderInfoBase)mImage.getTag();
        } else {
            iRenderInfoBase = new RenderInfoImageItem(mImage);
            mImage.setTag(iRenderInfoBase);
        }
        
        return iRenderInfoBase;
    }
    
    /**
     * 根据原始资讯类别生成不同的子view
     * @param type
     * @return
     */
    protected IRenderInfoBase getAppInfo(int type) {
        View v = null;
        LayoutParams params = null;
        IRenderInfoBase iRenderInfoBase = null;
        
        switch (type) {
            case AppInfo.TYPE_DISCUSS:
                if (null != mRoot.getTag() && mRoot.getTag() instanceof RenderInfoDiscuss) {
                    iRenderInfoBase = (IRenderInfoBase)mRoot.getTag();
                } else {
                    v = View.inflate(mRoot.getContext(), R.layout.item_view_info_discuss, null);
                    RenderInfoDiscuss renderInfoDiscuss = new RenderInfoDiscuss(v);
                    renderInfoDiscuss.setForward(true);
                    iRenderInfoBase = renderInfoDiscuss;
                }
                break;
            default:
                if (null != mRoot.getTag() && mRoot.getTag() instanceof RenderInfoOfficialBase) {
                    iRenderInfoBase = (IRenderInfoBase)mRoot.getTag();
                } else {
                    v = View.inflate(mRoot.getContext(), R.layout.item_view_info_official_base, null);
                    iRenderInfoBase = new RenderInfoOfficialBase(v);
                }
                
                break;
        }
        
        if (null != v) {
            // 设置生成子view背景
            params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.bottomMargin = mPadding;
            v.setBackgroundResource(R.drawable.bg_transpopo_main);
            v.setPadding(0, 2*mPadding, 0, 0);
            v.setId(ID);
            int count = ((LinearLayout)mRoot).getChildCount();
            if (count > 3) {
                ((LinearLayout)mRoot).removeViews(3, count-3);
            }
            ((LinearLayout)mRoot).addView(v, params);
            mRoot.setTag(iRenderInfoBase);
        }
        
        return iRenderInfoBase;
    }
    
    private void setAppInfo(int visibility) {
        if (null != mRoot.findViewById(ID)) {
            mRoot.findViewById(ID).setVisibility(visibility);
        }
    }
    @Override
    public void renderView(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        String value = appInfo.getDigest();
        if (bForward && null != appInfo.getUser()) {
            value = "@" + appInfo.getUser().getNickname() + " :" + value;
        }
        setContent(value);
        
        IRenderInfoBase iRenderInfoBase = null;
        
        iRenderInfoBase = getAudio();
        iRenderInfoBase.renderView(context, appInfo);
        
        iRenderInfoBase = getImage();
        iRenderInfoBase.renderView(context, appInfo);
        
        int visibility = View.GONE;
        // 判断是否有被转发的原始资讯
        if (null != appInfo.getInfo()) {
            iRenderInfoBase = getAppInfo(appInfo.getInfo().getInfoType());
            iRenderInfoBase.renderView(context, appInfo.getInfo());
            visibility = View.VISIBLE;
        } 
        setAppInfo(visibility);
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        setContent(appInfoDetail.getDiscuss());
        
        IRenderInfoBase iRenderInfoBase = null;
        
        iRenderInfoBase = getAudio();
        iRenderInfoBase.renderView(context, appInfoDetail);
        
        iRenderInfoBase = getImage();
        iRenderInfoBase.renderView(context, appInfoDetail);
        
        int visibility = View.GONE;
        if (null != appInfoDetail.getInfo()) {
            iRenderInfoBase = getAppInfo(appInfoDetail.getInfo().getInfoType());
            iRenderInfoBase.renderView(context, appInfoDetail.getInfo());
            visibility = View.VISIBLE;
        } 
        setAppInfo(visibility);
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }

    @Override
    public boolean onRichTextClick(View view, String richTxt, int type) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " richTxt=" + richTxt + " type=" + type);
        
        return true;
    }
}
